/******************************************************************************************************
Copyright (C) 2012 Mestrelab Research S.L. All rights reserved.

This file is part of the MNova scripting toolkit.

Authorized users of MNova Software may use this file freely, but this file is provided AS IS
with NO WARRANTY OF ANY KIND, INCLUDING THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE.
*****************************************************************************************************/

/*globals VerificationResult, NMRProcessing*/
/*jslint plusplus: true, indent: 4*/

function verificationCommon() {
	"use strict";
}

verificationCommon.reportFont = '<font style="font-size: 10pt; font-family: Times New Roman">';
verificationCommon.txtNMR1HVerifHeader = "\tFull\t1HPredCount\t1HCount";

verificationCommon.score2Color = function (aScoreQuality) {
	"use strict";

    if (!aScoreQuality) {
		return undefined;
	}
    var q = Math.round(aScoreQuality * 10);

    if (q >= 9) {
		return "#00CC00";
    } else if (q >= 8) {
		return "#33CC00";
    } else if (q >= 7) {
		return "#00E600";
    } else if (q >= 6) {
		return "#33F033";
    } else if (q >= 5) {
		return "#00FF00";
    } else if (q >= 4) {
		return "#66FF66";
    } else if (q >= 3) {
		return "#99FF99";
    } else if (q >= 2) {
		return "#CCFF99";
    } else if (q >= 1) {
		return "#FFFF99";
    } else if (q >= 0) {
		return "#FFFFCC";
    } else if (q >= -1) {
		return "#FFFF99";
    } else if (q >= -2) {
		return "#FFF099";
    } else if (q >= -3) {
		return "#FFC099";
    } else if (q >= -4) {
		return "#FF9980";
    } else if (q >= -5) {
		return "#FF6666";
    } else if (q >= -6) {
		return "#FF3333";
    } else if (q >= -7) {
		return "#FF0000";
    } else if (q >= -8) {
		return "#F80000";
    } else if (q >= -9) {
		return "#E80000";
    } else if (q >= -10) {
		return "#E00000";
    } else {
		return "#FF0000";
	}
};

verificationCommon.verTestDistance = function (aVerTestRes) {
	"use strict";

	if (aVerTestRes === undefined) {
		return undefined;
	}

	if (aVerTestRes.data !== undefined) {
		if (aVerTestRes.data.length > 0) {
			return aVerTestRes.data[0].toPrecision(6);
		}
	}

	return "N/A";
};

verificationCommon.verTestScore = function (aVerTestRes) {
	"use strict";

	if (aVerTestRes && aVerTestRes.result !== undefined) {
		return aVerTestRes.result.toPrecision(2);
	}
	return undefined;
};

verificationCommon.verTestQuality = function (aVerTestRes) {
	"use strict";

	if (aVerTestRes && aVerTestRes.quality !== undefined) {
		return aVerTestRes.quality.toPrecision(2);
	}
	return undefined;
};

verificationCommon.verTestSignificance = function (aVerTestRes) {
	"use strict";

	if (aVerTestRes && aVerTestRes.significance !== undefined) {
		return (aVerTestRes.significance.toPrecision(2));
	}
	return undefined;
};

verificationCommon.verTestName = function (aVerTestRes) {
	"use strict";

	if (aVerTestRes === undefined) {
		return undefined;
	}

	switch (aVerTestRes.id) {
	case VerificationResult.Tests.H1NN:
		return "H1NN";
	case VerificationResult.Tests.H1NM:
		return "1H Multiplets Count";
	case VerificationResult.Tests.H1NP:
		return "H1NP";
	case VerificationResult.Tests.H1PBM:
		return "H1PBM";
	case VerificationResult.Tests.H1MC:
		return "H1MC";
	case VerificationResult.Tests.H1ASSIGN:
		return "1H Assignments";
	case VerificationResult.Tests.H1PBM_L:
		return "H1PBM_L";
	case VerificationResult.Tests.HSQC13C_NN:
		return "HSQC13C_NN";
	case VerificationResult.Tests.HSQC13C_NP:
		return "HSQC13C_NP";
	case VerificationResult.Tests.HSQC13C_PBM:
		return "HSQC13C_PBM";
	case VerificationResult.Tests.HSQC_XCOR:
		return "HSQC x-corr";
	case VerificationResult.Tests.MS_MolMatch:
		return "MSMatch";
	case VerificationResult.Tests.MS_MolMatch_Similarity:
		return "MSSim";
	case VerificationResult.Tests.MS_MolMatch_Purity:
		return "MSPur";
	case VerificationResult.Tests.H1APS:
		return "Aromatic Pattern Substitution";
	default:
		break;
	}
	return "T" + aVerTestRes.id;
};

verificationCommon.verGetMolFileName = function (aMolfile) {
	"use strict";

	var slashIndex = aMolfile.lastIndexOf("/"),
		extIndex = aMolfile.lastIndexOf(".mol");

	return aMolfile.slice(slashIndex + 1, extIndex);
};

verificationCommon.verResReport = function (aVerRes) {
	"use strict";

	var t, test, resTable, testCell, today,
		res = {};

	res.global = aVerRes ? aVerRes.result.toPrecision(2) : -1.0;
	res.globalQuality = aVerRes ? aVerRes.quality.toPrecision(2) : -1.0;
	res.globalSignificance = aVerRes ? aVerRes.significance.toPrecision(2) : -1.0;
	res.globalColor = verificationCommon.score2Color(res.globalQuality);
	res.tests = [];

	res.hsqcTestsDone = false;
	res.h1TestsDone = false;
	res.msTestDone = false;

	if (aVerRes) {
		for (t in aVerRes.testsResults) {
			if (typeof aVerRes.testsResults[t] === 'object') {
				test = {};
				test.score = verificationCommon.verTestScore(aVerRes.testsResults[t]);
				test.significance = verificationCommon.verTestSignificance(aVerRes.testsResults[t]);
				test.quality = verificationCommon.verTestQuality(aVerRes.testsResults[t]);
				test.color = verificationCommon.score2Color(test.quality);
				test.distance = verificationCommon.verTestDistance(aVerRes.testsResults[t]);
				test.name = verificationCommon.verTestName(aVerRes.testsResults[t]);
				if (aVerRes.testsResults[t].id === VerificationResult.Tests.HSQC_13C) {
					res.hsqTestsDone = true;
				}
				if (aVerRes.testsResults[t].id === VerificationResult.Tests.FullComp) {
					res.h1TestsDone = true;
				}
				if (aVerRes.testsResults[t].id === VerificationResult.Tests.MS_MolMatch) {
					res.msTestDone = true;
				}
				res.tests.push(test);
			}
		}
	}

	resTable = "";
	resTable = verificationCommon.reportFont + "<table><tr><td colspan=\"$TestsCount$\" bgcolor=\"$GlobalColor$\"> Score: $Global$</td><td colspan=\"$TestsCount$\" bgcolor=\"$GlobalColor$\"> Significance: $GlobalSignificance$</td><td colspan=\"$TestsCount$\" bgcolor=\"$GlobalColor$\"> Quality Factor: $GlobalQuality$</td></tr><tr>";
	resTable = resTable.replace("$TestsCount$", res.tests.length).replace("$GlobalColor$", res.globalColor).replace("$Global$", res.global).replace("$TestsCount$", res.tests.length).replace("$GlobalColor$", res.globalColor).replace("$GlobalSignificance$", res.globalSignificance).replace("$TestsCount$", res.tests.length).replace("$GlobalColor$", res.globalColor).replace("$GlobalQuality$", res.globalQuality);

	for (t in res.tests) {
		if (res.tests.hasOwnProperty(t)) {
			test = res.tests[t];
			if (typeof test === 'object') {
				testCell = "<td bgcolor=\"$TestColor$\">$TestName$ Score: $TestValue$</td><td bgcolor=\"$TestColor$\">$TestName$ Significance: $TestSignificance$</td><td bgcolor=\"$TestColor$\">$TestName$ Quality Factor: $TestQuality$</td>";
				resTable += testCell.replace("$TestColor$", test.color).replace("$TestName$", test.name).replace("$TestValue$", test.score).replace("$TestColor$", test.color).replace("$TestName$", test.name).replace("$TestSignificance$", test.significance).replace("$TestColor$", test.color).replace("$TestName$", test.name).replace("$TestQuality$", test.quality);
			}
		}
	}
	resTable += "</tr></table></font>";

	res.htmlTable = resTable;

	today = new Date();
	res.todayStr = "<b>" + verificationCommon.reportFont + today.toISOString() + "</font></b>";

	return res;
};

verificationCommon.processH1ForVerification = function (h1, aH1Proc, aSolvent) {
	"use strict";
	var reprocess = false,
		proc;

	if (h1 === undefined || !h1.isValid()) {
		return;
	}

	if (aSolvent && aSolvent.length) {
		h1.solvent = aSolvent;
	}

	if (aH1Proc) {
		proc = new NMRProcessing(aH1Proc);
		reprocess = true;
	}

	if (!proc) {
		proc = new NMRProcessing(h1.proc);
	}

	if (reprocess) {
		h1.process(proc);
	}
};

verificationCommon.processHsqcForVerification = function (hsqc, aHsqcProc, aSolvent) {
	"use strict";
	var proc,
		reprocess = false;

	if (hsqc === undefined || !hsqc.isValid()) {
		return;
	}

	if (aSolvent && aSolvent.length) {
		hsqc.solvent = aSolvent;
	}

	if (aHsqcProc) {
		proc = new NMRProcessing(aHsqcProc);
		reprocess = true;
	}

	if (reprocess) {
		hsqc.process(proc);
	}
};

verificationCommon.specGroupsCount = function (h1Files, hsqcFiles) {
	"use strict";

	if (h1Files !== undefined && hsqcFiles !== undefined && h1Files.length !== hsqcFiles.length) {
		return 0;
	} else if (h1Files !== undefined) {
		return h1Files.length;
	} else if (hsqcFiles !== undefined) {
		return hsqcFiles.length;
	}
	return 0;
};

verificationCommon.verificationType = function (h1, hsqc, mass) {
	"use strict";

	var type = "";

	if (h1 !== undefined) {
		type += "1H";
	}

	if (hsqc !== undefined) {
		if (type !== "") {
		    type += "+";
		}
		type += "HSQC";
	}
	if (mass !== undefined) {
		type += "+MS";
	}

	return type;
};
